import requests
import pymysql

f=open("password.txt","r")
password=f.read()
f.close()
password=password.replace("\n","")
conn = pymysql.connect(host='localhost', user='root',
                       password=password, database='teamfight_wiki', port=3306)
cursor = conn.cursor()
base_url = "https://game.gtimg.cn/images/lol/act/img/tft/js/"
race_url = base_url + "race.js"
job_url = base_url + "job.js"
f = open("羁绊.txt", "w",encoding="utf-8")
count = 0
for url in [race_url, job_url]:
    response = requests.get(url)
    data = response.json()
    for i in data["data"]:
        count += 1
        description = ""
        trammel_number = ""
        description += i["imagePath"] + "$"
        description += i["introduce"] + " "
        for level in i["level"]:
            if level != list(i["level"])[0]:
                trammel_number += "/"
            description += f"{level}个：{i['level'][level]}  "
            trammel_number += f"{level}"
        description = description.replace("\n", " ")
        description = description.replace("\r", " ")
        if len(description) > 200:
            description = description[:200]
        print(count, i["name"], description, trammel_number)
        f.write(str(count) + "^" + i["name"] + "^" + description + "^" + trammel_number + '\n')
        sql = f"""INSERT INTO trammel VALUES({count},"{i["name"]}","{description}","{trammel_number}");"""
        cursor.execute(sql)
conn.commit()
